﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Windows.Media;

namespace OpenLS.Spreadsheet.Formats.Biff
{
    abstract class Palette
    {
        public abstract bool ContainsIndex(int index);
        public abstract Color GetColor(int index);

        protected double ColorDistance(Color value, Color color)
        {
            double dr = value.R - color.R;
            double dg = value.G - color.G;
            double db = value.B - color.B;
            return dr*dr + dg*dg + db*db;
            
        }

        protected internal abstract IEnumerable<KeyValuePair<int, Color>> GetIndexColors();

        public int GetClosestIndex(Color color)
        {
            double minDistance = double.MaxValue;
            int result = 0;
            foreach (var item in GetIndexColors())
            {
                double d = ColorDistance(item.Value, color);
                if (d < minDistance)
                {
                    minDistance = d;
                    result = item.Key;
                }
            }
            return result;
        }
    }
    class SinglePalette: Palette
    {

        public SinglePalette(IEnumerable<Pair<int, Color>> items)
        {
            foreach (var item in items)
            {
                _table.Add(item.First, item.Second);
            }
        }
        Dictionary<int, Color> _table = new Dictionary<int, Color>();

        public override bool ContainsIndex(int index)
        {
            return _table.ContainsKey(index);
        }

        public override Color GetColor(int index)
        {
            return _table[index];
        }

        protected internal override IEnumerable<KeyValuePair<int, Color>> GetIndexColors()
        {
            return _table;
        }
    }
}